package com.nzd.slsharing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nzd.slsharing.pojo.*;

import com.nzd.slsharing.service.PostSectionService;
import com.nzd.slsharing.service.SuggestionService;
import com.nzd.slsharing.service.UserService;
import com.nzd.slsharing.util.AuthContextHolder;
import com.nzd.slsharing.util.JwtHelper;
import com.nzd.slsharing.util.MD5;
import com.nzd.slsharing.util.Result;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

import static com.nzd.slsharing.util.ResultCodeEnum.ARGUMENT_VALID_ERROR;
import static com.nzd.slsharing.util.ResultCodeEnum.LOGIN_NOUSER;

@RestController
@RequestMapping("/api/suggestion")
public class SuggestionController {
    @Resource
    private SuggestionService suggestionService;
    //分类
    @Resource
    private PostSectionService postSectionService;


    @PostMapping("/suggestion")
    public Result suggestions(@RequestBody Suggestion suggestion) {
        //检验参数
        if(suggestion.getTitle().isEmpty()||suggestion.getContent().isEmpty()|suggestion.getCode()==null|suggestion.getUserName().isEmpty()|suggestion.getUserId().isEmpty()){
            return Result.fail(ARGUMENT_VALID_ERROR);
        }
        //根据提交的code查找分类
        PostSection postSection=postSectionService.getOne(new QueryWrapper<PostSection>().eq("type_code",suggestion.getCode()));
        if(postSection==null){
            return Result.fail("分类为空！");
        }
        suggestion.setName(postSection.getTypeName());
        suggestion.setCreateTime(new Date());
        suggestion.setStatus(0);
        suggestion.setCreateTime(new Date());
        return Result.ok(suggestionService.save(suggestion));
    }

    @GetMapping("/list")
    public Result list(@RequestParam String stuId){
        QueryWrapper<Suggestion> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",stuId);
        return Result.ok(suggestionService.list(queryWrapper));
    }

    @GetMapping("/pageList")
    public Result pageListlist(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(required = false, defaultValue = "3") Integer pageSize){
        // 创建分页对象
        Page<Suggestion> page = new Page<>(pageNum, pageSize);
        return Result.ok(suggestionService.page(page));
    }

    @PutMapping("/suggestion")
    public Result Reply (@RequestBody Suggestion suggestion) {
        suggestion.setStatus(1);
        return Result.ok(suggestionService.updateById(suggestion));
    }

    @DeleteMapping("/{id}")
    public Result Reply (@PathVariable Integer id) {
        return Result.ok(suggestionService.removeById(id));
    }
}
